import Toast from '@vant/weapp/toast/toast';
let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: '',
    id: 0,
    order: 1,
    info: {},
    process: 0,
    total: 100,
    had_click: '',
    right_color: 'red',
    disable_show: false,
    rel: false,
    count: false,
    had_a: false,
    is_all: false,
    bottom_p: 'absolute',
    current: '',
    show:false
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
    }

    if (options.count) {

      this.setData({
        total: options.count,
        count: true
      })
    }

    if (options.is_all) {
      this.setData({
        is_all: options.is_all
      })
    }

    this.get_info(options.is_first, this.data.is_all);

  },
  onChange(event) {
    let that = this
    this.setData({
      radio: event.detail,
    });
    this.setData({
      had_a: true,
      is_all: that.data.is_all
    })

    if (this.data.radio == this.data.info.answer_key) {
      this.setData({
        id: Number(this.data.id) + 1,
        // radio: ,
        had_click: 1,
        right_color: 'green',

      })
      setTimeout(() => {
        if(that.checkComplete()){
          return false;
        }
        this.get_info(2, that.data.is_all);
        this.setData({
          radio: '',
          had_click: 0
        })
      }, 500);
    } else {
      this.setData({
        disable_show: true,
        rel: true,
        show:true
      })
      that.add_wrong();
      if(that.checkComplete()){
        return false;
      }
      // console.log(that.data.info.explain.length,333333,that.data.rel)
      // if (that.data.rel == true&&that.data.info.explain.length>=150) {
      //   that.setData({
      //     bottom_p: 'none'
      //   })
      // }
    }
  },
  onClick(event) {
    const val = event.currentTarget.dataset.answer;

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  get_info: function (is_first, is_all) {
    var that = this;
    //请求查询
    wx.showLoading({
      title: '加载中',
    });
    App._get('driver_detail', {
      id: that.data.id,
      next: that.data.order,
      is_first: is_first,
      is_all: is_all
    }, result => {

      if (result.code == 0) {

        that.setData({
          current: result.data.current,
          bottom_p: 'absolute',
          show:false
        })

        if (result.data.info.options.length == 0) {
          result.data.info.options = ['对', '错']
        }
        that.setData({
          info: result.data.info,
        })
        if (!this.data.count) {
          that.setData({
            total: result.data.count
          })
        }

        if (that.data.info.pic != '') {
          that.setData({
            bottom_p: 'none'
          })
        }

        that.setData({
          process: ((that.data.current / Number(that.data.total).toFixed(3)) * 100).toFixed(1)
        })
      } else {
        App.showSuccess("加载失败!")
      }
    }, function () {}, function () {

      wx.hideLoading();
    });

  },
  next: function () {

    if (!this.data.had_a) {
      Toast.fail('请先答题');
      return false;
    }
    this.setData({
      id: Number(this.data.id) + 1,
    })

    wx.redirectTo({
      url: '/pages/driver/list?id=' + this.data.id + "&count=" + this.data.total + "&is_first=2&is_all=" + this.data.is_all,
    })
  },
  add_wrong: function () {
    let _this = this;

    if (!App.checkIsLogin()) {

      wx.navigateTo({
        url: '/pages/login/login',
      })
      return false;
    }
    App._post_form('add_driver_wrong', {
      id: _this.data.info.id
    }, function (res) {
      // if (res.code == 0) {
      //   Toast.success('加入成功')
      // }
    })
  },

  onClose:function(){
    this.setData({
      show:false
    })
    console.log(this.data.total,this.data.current,this.checkComplete(),222)
    if(this.checkComplete()){
      return false;
    }
    this.next();
  },
  checkComplete:function(){
    let that=this;
    if(that.data.total==that.data.current){
      Toast('答题完毕')
      setTimeout(function(){
          wx.switchTab({
            url: '/pages/index/index',
          })
      },2500)
    
    }
    return false;
  }
})